//
//  VisitorLoginView.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//

import UIKit

import SnapKit

//未登录页面部分

//设置代理
@objc protocol VisitorLoginViewDelegate : NSObjectProtocol{
    
    
    //协议方法,代理对象 ,optional是可选的意思,下面用的时候要加?
    optional func visitorWillLogin()
    optional func visitorWillRegister()
    
}
//懒加载所有控件 实现代理方法
class VisitorLoginView: UIView {
    
    //声明代理
    weak  var visitorDelegate : VisitorLoginViewDelegate?
    
    //登录 ,协议代理
    @objc func loginDidBtnClick(){
        
        visitorDelegate?.visitorWillLogin?()
        
    }
    //注册 ,协议代理
    @objc func registerBtnDidClick(){
        
        
        visitorDelegate?.visitorWillRegister?()
        
    }
    //设置页面信息

          func setUIInfo(title: String,imageName: String?) {
//          //旋转的图片除了首页其他不用
            iconView.hidden = false
            tipLabel.text = title
//            //如果没有图片的话
            if imageName != nil{
               circleView.image = UIImage(named:imageName!)
                bringSubviewToFront(circleView)
                iconView .hidden = true
            }
            else{
//
                startAnimation()
            }
    
        }

    //设置旋转的视图
    
    private func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT //无限循环
        anim.duration = 20 //持续时间
        anim.removedOnCompletion = false //跳转什么的都不停止运行
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    //重写构造
    /*-----------------------------*/
    override init(frame: CGRect) {
        super.init(frame: CGRectZero)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*-----------------------------*/

    
    //懒加载添加控件
    
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //添加登录注册按钮
    
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
        
        }()
    
    private lazy var registerBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        return btn
        
        }()
    
    //添加说明文字
    
    private lazy var tipLabel: UILabel = {
        
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(20)
        lab.numberOfLines = 0 //换行
        lab.textColor = UIColor.lightGrayColor()
        lab.text = "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = NSTextAlignment.Center
        
        return lab
        }()
  
    
//添加控件
    private func setupUI(){
        
        
//        先view 后label ,否则会被遮盖的
        addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        addSubview(tipLabel)
        
        //布局部分
//    先设置frame布局失效,这个很有必要
    for vi in subviews {
    vi.translatesAutoresizingMaskIntoConstraints = false
    
    }

//    格式:
    // 注意: 布局的约束 尽量相对一个控件
    //圆圈的约束
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //大图标
addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //文字部分位置约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 25))
        
//宽高约束 , 要么字体小一点,要么高度设置大一点,不然显示不全
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 150))
        
//设置登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))

        
        //设置注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))

        //背景视图虚幻部分
        
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -70))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)

        
//    按钮点击事件
        
        loginBtn.addTarget(self, action: "loginDidBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
}
